home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / obsolete / equal_variance.pro < prev    next >
Text File  |  1997-07-08  |  10KB  |  352 lines

  1. ; $Id: equal_variance.pro,v 1.3 1997/01/15 04:02:19 ali Exp $
  2. ;
  3. ;  Copyright (c) 1991-1997, Research Systems Inc.  All rights
  4. ;  reserved. Unauthorized reproduction prohibited.
  5.  
  6.  
  7.  pro equal_variance, X1, FC, DF, P, One_Way = One,  $
  8.                      Unequal_One_Way = Unequal,$
  9.                      Two_Way = Two, Interactions_Two_Way  = $
  10.                      Interactions, Hartley = Hart, Bartlett = Bart, $
  11.                      Box = Bx, Levene = Lev, Missing =M,      $
  12.                      Group_No = gn, Block = B,  TInteraction=IN
  13. ;+
  14. ; NAME:
  15. ;    EQUAL_VARIANCE
  16. ;
  17. ; PURPOSE: 
  18. ;    To test treatments, blocks and interactions, as applicable, for 
  19. ;    variance equality before performing an anova test.
  20. ; CATEGORY:
  21. ;    Statisitics.
  22. ;
  23. ; CALLING SEQUENCE:
  24. ;    EQUAL_VARIANCE, X [, FC, DF, P]
  25. ;
  26. ; INPUTS:  
  27. ;    X:    two or three dimensional array of experimental data.
  28. ;
  29. ; KEYWORDS: 
  30. ;    BLOCK:    a flag, if set by user, to test homogeneity of block 
  31. ;        variances. Default is treatment variances.  Block should 
  32. ;        only be set when either the keyword TWO_WAY or 
  33. ;        INTERACTIONS_TWO_WAY is set.
  34. ;
  35. ; TINTERACTION:    a flag, if set by the user, to test homogeneity of variance
  36. ;        for all treatment/block combinations.
  37. ;
  38. ; Type of design structure. Options are
  39. ;      ONE_WAY:    if set,  1-way anova.
  40. ;
  41. ; UNEQUAL_ONE_WAY:  if set,  1-way ANOVA with unequal sample sizes.
  42. ;
  43. ; TWO_WAY:    if set,  2-way ANOVA.
  44. ;
  45. ; INTERACTIONS_TWO_WAY:  if set,  2-way ANOVA with interactions.
  46. ;
  47. ; One and only one of the above options may be set.
  48. ; Type of test to be used to test variance equality.
  49. ; Options are:
  50. ;              
  51. ;      HARTLEY:    Hartley's F-Max test. Sample sizes should be equal and data
  52. ;        nearly normally distributed.
  53. ;
  54. ;     BARTLETT:    Bartlett's test for nearly normally distributed data and not
  55. ;        necessarily equal sample sizes.
  56. ;
  57. ;    BOX:    Box's test. Robust for large data sets, data not necessarily
  58. ;        normally distributed.
  59. ;    
  60. ;    LEVENE:    Levene's test.              
  61. ;
  62. ; One and only one test may be selected.
  63. ;
  64. ;      MISSING:    place holder for missing data or undefined if no data is 
  65. ;        missing.
  66. ;
  67. ;     GROUP_NO:    number of groups to use in Box test.
  68. ;
  69. ; OUTPUT:     
  70. ;    FC:    value of statistic computed by each test.  If Bartlett's is 
  71. ;        selected, FC has chi square distribution.  Otherwise, FC has 
  72. ;        F distribution.
  73. ;
  74. ;    DF:    degrees of freedom. 
  75. ;        Hartley: DF = [max sample size -1, number of treatments],
  76. ;        Bartlett: DF = number of treatments,
  77. ;        Box,Levene: DF =[numertor DF,denominator DF]
  78. ;
  79. ;    P:    probability of FC or something greater for Barlett's, Box's 
  80. ;        and Levene's Test.  P = -1 for Hartley's.
  81. ;
  82. ; PROCEDURE:
  83. ;    Milliken and Johnson, "Analysis of Messy Data", Van Nostrand, 
  84. ;    Reinhold, 1984, pp. 18-22.
  85. ;-
  86.  On_Error,2
  87.  
  88.  SX = size(X1)
  89.  
  90.           ;Ascertain design structure.
  91.  ONE_WAY = 0 & ONE_WAY_UNEQUAL=1 & TWO_WAY = 2 & TWO_WAY_INTERACTIONS =3
  92.  keyset = [keyword_set(One), keyword_set(Unequal), $
  93.            keyword_set(Two),keyword_set(Interactions)]
  94.  T  = where(keyset,nkey)     
  95.  
  96.  if nkey NE 1 THEN BEGIN
  97.    print,'equal_variance - must specify one and only one type'
  98.    print, '                of design structure'
  99.   return
  100.  ENDIF
  101.  
  102.  T = T(0)               ;T = selected design structure
  103.  
  104.  if((T EQ TWO_WAY_INTERACTIONS) AND  (SX(0) NE 3) OR $
  105.      ( T NE TWO_WAY_INTERACTIONS AND (SX(0) NE 2))) THEN BEGIN
  106.       print, 'Anova- wrong number of dimensions' 
  107.       return
  108.  ENDIF
  109.  
  110.  
  111.          ; Ascertain test type
  112.  Hartley = 0 & Bartlett = 1 & Box = 2 & Levene = 3
  113.  keyset = [keyword_set(Hart), keyword_set(Bart), $
  114.            keyword_set(Bx),keyword_set(Lev)]
  115.  VT  = where(keyset,nkey)
  116.  
  117.  if nkey NE 1 THEN $
  118.     print, $
  119.       'equal_variance - must specify one and only test type'
  120.  
  121.  VT = VT(0)             ; VT = selected test type
  122.  
  123.  X = float(X1)
  124.  
  125.  SX=Size(X)        
  126.  C=SX(1)      ; Compute number of columns       
  127.  D= 1         ; Default # of replications
  128.  
  129.  
  130.  
  131.  
  132.  if( T EQ TWO_WAY_INTERACTIONS) THEN R = SX(3) else R = SX(2)
  133.  
  134.                                  ;Compute number of rows
  135.  
  136.  
  137.  if R LT 2 OR C LT 2 THEN BEGIN
  138.    print,'equal_variance- data array needs more rows or columns'
  139.    return
  140.  ENDIF
  141.  
  142.  if KEYWORD_SET(B) THEN $
  143.   if T EQ TWO_WAY THEN BEGIN
  144.     X = transpose(X)
  145.     C = R
  146.     R = SX(1)
  147.  ENDIF ELSE if T EQ ONE_WAY or T EQ ONE_WAY_UNEQUAL THEN BEGIN
  148.     print, 'equal_variance- Block test only done with'
  149.     print, '                two_way design structures'
  150.     return
  151.  ENDIF
  152.  
  153. ; Pre-process three dimensional array by converting to a
  154. ; two dimensional array with R*C columns and D rows.
  155.  
  156.  
  157.  if(T EQ TWO_WAY_INTERACTIONS) THEN BEGIN    
  158.  
  159.      D = SX(2)
  160.      if KEYWORD_SET(IN) THEN BEGIN
  161.       X = Fltarr(R*C,D)
  162.       for i = 0L,R*C-1  DO X( i,*) = X1(i mod C,*,i / C) 
  163.       X = float(x)
  164.       C=R*C
  165.  
  166.       R=D
  167.     ENDIF ELSE BEGIN 
  168.         T = ONE_WAY
  169.         if KEYWORD_SET(B) THEN BEGIN
  170.           X = reform(X,C*D,R)
  171.           X = transpose(X)
  172.           temp = C*D
  173.           C = R
  174.           R = temp
  175.         ENDIF ELSE BEGIN
  176.           X = reform(X,C,R*D)
  177.           R = R*D
  178.           D = 1
  179.         ENDELSE
  180.    ENDELSE
  181.  ENDIF
  182.  
  183.  
  184. if (N_Elements(M) NE 0) THEN BEGIN
  185.    if (T EQ TWO_WAY) THEN BEGIN
  186.        print,            $
  187.         "equal_variance- two_way anova without interactions"
  188.        print,"                can't have missing data."
  189.        return
  190.     ENDIF
  191.  
  192.     Pairwise,X,M,YR,YC,NotGood, good               
  193.                                  ; replace missing data by 0,return
  194.                                  ;vectors YR,YC of row,column sizes
  195.      if(N_Elements(NotGood) EQ 0) THEN M=0
  196.        CN = where(YC NE 0,NYC)
  197.        RN = where(YR NE 0,NYR)
  198.        CN1 = where(YC lt 2, NYC1)
  199.        RN1 = where(YR lt 2,NYR1)
  200.  
  201.      if NYC LT 2  OR $
  202.         NYC1  NE 0 THEN BEGIN
  203.        print,"equal_variances, too many missing entries"
  204.       return 
  205.     ENDIF
  206.   ENDIF ELSE M=0
  207.    
  208.  if (VT EQ BARTLETT OR VT EQ HARTLEY ) THEN BEGIN    
  209.                       
  210.     ;compute column variances
  211.  if (M EQ 0) THEN BEGIN
  212.     Mean = (X # replicate(1.0, R))/R
  213.     Var = (X -  MEAN # replicate(1.0, R))^2 
  214.     Var =  Var # replicate(1.0,R)
  215.     Var = Var / (R-1)
  216.  
  217. ;    if (T EQ TWO_WAY) THEN BEGIN       ;add on row variances 
  218. ;       Mean = (replicate(1.0, C) # X)/C
  219. ;       RVAR = (X -  replicate(1.0, C) # MEAN)^2 
  220. ;       RVAR = Replicate(1.0,C) # RVAR
  221. ;      RVar = RVar /(C-1)
  222. ;       Var = [Var, transpose(RVar)]
  223. ;      ENDIF
  224.  
  225.  ENDIF ELSE BEGIN
  226.  
  227.         Mean =  (X # Replicate(1.,R)) /YC  
  228.         Mean = MEAN # replicate(1.0, R)
  229.         VAR = fltarr(C,R)
  230.         VAR(good) = (X(good) - Mean(good))^2
  231.         VAR = VAR # Replicate(1.0,R)
  232.         YC  = YC - 1
  233.         Var = Var/YC
  234.  
  235.      ENDELSE
  236.  
  237.   here = where(VAR eq 0, count)
  238.   if count ne 0 THEN BEGIN
  239.      print, 'equal_variance- cannot handle populations with '
  240.      print, '                0 variance'
  241.      return
  242.   ENDIF
  243.  
  244.  ENDIF
  245.  
  246.  
  247.  
  248.  case VT of
  249.  
  250.  HARTLEY : BEGIN
  251.               FC = max(Var)/min(Var) 
  252.               if (M EQ 0) THEN DF = [R-1,C] else DF = [max(YC),C] 
  253.               P = -1
  254.              END
  255.  BARTLETT: BEGIN
  256.              if M NE 0   THEN v = Total(YC) ELSE BEGIN
  257.                v = R*C-C
  258.                YC = replicate (R-1,C)
  259.                CN  = replicate(1.0,C)
  260.              ENDELSE
  261.              t1 = N_Elements(Var) -1.0
  262.              CC = 1. + 1.0/(3*t1) *  $
  263.                                        (Total(1./YC(CN)) - 1./v)
  264.              s2 = Total(YC*Var)/v
  265.              FC = 1/CC * (v * Alog(s2) - Total(YC* Alog(Var)))
  266.              DF = t1
  267.              P  = 1. - chi_sqr1(FC,DF)
  268.  
  269.              END
  270.  
  271.  BOX:     BEGIN
  272.             if n_elements(gn) EQ 0 THEN gn = 2
  273.  
  274.             if gn gt R then BEGIN
  275.                print, "equal_variance- group size is too large"
  276.                return
  277.             ENDIF
  278.  
  279.             group = fix(gn * randomu(seed,C,R))    
  280.                                         ; break columns into groups
  281.             if (M NE 0) THEN group(NotGood) = -1     
  282.             Var =  Fltarr (C,gn)
  283.             for i = 0L,gn-1 DO BEGIN    
  284.                                ;compute group variances for columns
  285.               tempgroup = X
  286.               ngri = where ( group ne i)
  287.               tempgroup(ngri) = 0
  288.               gri = where(group eq i)
  289.               tempgroup (gri) =1
  290.               ysize = tempgroup # Replicate(1,R)
  291.               ysize1 = ysize
  292.               y = where(ysize ne 0,count)
  293.               if count GT 0 then BEGIN
  294.                 ysize(y) = 1./ysize(y) 
  295.                 ysize1(y) = ysize1(y) -1
  296.                ENDIF
  297.  
  298.                 tempgroup(gri) = X(gri)
  299.                 Var1 = (tempgroup*tempgroup) # Replicate (1.,R) -$
  300.                          (tempgroup # Replicate(1.,R))^2 * ysize
  301.  
  302.                 y = where(ysize1 ne 0,count)
  303.                 if count NE 0 THEN ysize1(y) = 1/ysize1(y)
  304.                 Var1 = Var1 * ysize1
  305.                 Var(*,i) = Var1        
  306.                                ;append variances for ith group
  307.             ENDFOR          
  308.             
  309.             DF = [C-1,C*gn-C]
  310.            not0 = where (var ne 0,count)
  311.            if count NE 0 THEN var(not0) = alog(var(not0))
  312.            FC = 1
  313.            anova,var,FCTest = FC, ONE_WAY=1  ,/No_Printout
  314.            P = 1 - f_test1(FC, C-1, C*gn-C)
  315.             END
  316.  
  317. LEVENE :  BEGIN
  318.             if ( M EQ 0) THEN BEGIN
  319.                 YC = Replicate (R-1,C)
  320.                 ysize = 1./YC
  321.             ENDIF  else begin
  322.                 y = where(YC NE 0,count)
  323.                 ysize = YC 
  324.                if count GT 0 then ysize(y) = 1./ysize(y)
  325.                 
  326.              ENDELSE
  327.        
  328.             Dev= ABS(X - ((X # Replicate (1.,R))*ysize) #     $
  329.                                            Replicate(1.,R))
  330.             if (M NE 0) THEN BEGIN
  331.                Dev(NotGood) = -1
  332.                tot = total(YC)
  333.             ENDIF ELSE tot = R*C   
  334.  
  335.             DF = [C-1,tot-C]
  336.             FC=1
  337.            if ( M NE 0) THEN      $
  338.                anova,Dev,FCTest = FC,      $
  339.             Unequal_ONE_WAY=1,Missing=-1,  /No_Print $
  340.            ELSE  anova,FCTest = FC,Dev,ONE_WAY=1, $
  341.                       /No_Print          
  342.             P = 1 - f_test1(FC, C-1, tot-1)
  343.             END
  344.             
  345.      
  346.  ELSE :
  347.  ENDCASE
  348.  
  349. RETURN
  350. END 
  351.